import {
  setUpUser, uploader
} from '../../config/https'

import Toast from '../../miniprogram_npm/_vant-weapp@0.5.7@vant-weapp/toast/toast';

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show:false,
    index:'',
    options: '',
    columns: ['女','男']
  },
  onConfirm(event) {
    const { index } = event.detail;
    this.setData({
      index
    })
    this.onEide()
    this.onClose()
  },

  onCancel() {
    Toast('取消');
    this.onClose()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      options: options,
      index: options.sex
    })
  },
  onClose(){
    var show =!this.data.show
    this.setData({
      show
    })
  },
  //修改用户昵称
  onInputName(e) {
    var options = this.data.options;
    options.nickname = e.detail.value;
    this.setData({
      options
    })
    this.onEide()
  },
  //修改图片
  onUpimg() {
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        console.log(app)
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        //上传图片接口
        wx.uploadFile({
          url: app.globalData.upImage,
          filePath: tempFilePaths[0],
          name: 'file',
          method:'POST',
          formData: {
            'user': 'test'
          },
          success(res) {
            let dataPath = JSON.parse(res.data)
            console.log(dataPath)
            var options = that.data.options
            options.avatar = dataPath.url
            that.setData({
              options
            })
            that.onEide();
            const data = res.data
            //do something
          },fail(err){console.log(err)},
        })
       
        
      }
    })
  },
  // 封装函数
  onEide() {
    if (this.data.options.nickname==''){
      Toast('姓名不能为空')
    }else{
      setUpUser({
        nickname: this.data.options.nickname,
        avatar: this.data.options.avatar,
        gender: this.data.index
      }).then(res => {
        Toast("修改成功")
      }).catch(err => {
        Toast(err.message)
      })
    }
   
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})